package svc

import (
	"bigmodel/internal/config"
	"bigmodel/internal/middleware"
	"bigmodel/service/chat"
	"bigmodel/service/chat/spark"
	tool "bigmodel/tools"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"github.com/zeromicro/go-zero/rest"
)

type ServiceContext struct {
	Config config.Config
	Orm sqlx.SqlConn
	Cors rest.Middleware
}

func NewServiceContext(c config.Config) *ServiceContext {
	spk := c.Spark
	sparkpms := make([]spark.Client, 0)
	for _, val := range c.Spark.AppParams {
		sparkpms = append(sparkpms, *spark.MustNewClient(spk.AppID, val.APIKEY, val.APISECRET))
	}
	aliKeys := c.TongYi.ApiKeys

	err := chat.InitPool(&chat.ChatResourceFactoryParams{
		Sparks: sparkpms,
		AKeys: aliKeys,
		Cache:  tool.NewZeroCache(c.Cache),
	})
	logx.Must(err)
	return &ServiceContext{
		Config: c,
		Orm: sqlx.NewSqlConn("postgres",c.DB.DataSource),
		Cors: middleware.NewCommonJwtAuthMiddleware().Handle,
	}
}
